package com.castle.fortress.admin.jmreport.service.impl;

import cn.hutool.core.util.StrUtil;

import com.castle.fortress.admin.system.dto.SysUserDto;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.jmreport.entity.JimuReportEntity;
import com.castle.fortress.admin.jmreport.dto.JimuReportDto;
import com.castle.fortress.admin.jmreport.mapper.JimuReportMapper;
import com.castle.fortress.admin.jmreport.service.CastleJimuReportService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.*;

/**
 * 在线excel设计器 服务实现类
 *
 * @author castle
 * @since 2023-10-11
 */
@Service
public class CastleJimuReportServiceImpl extends ServiceImpl<JimuReportMapper, JimuReportEntity> implements CastleJimuReportService {
    /**
    * 获取查询条件
    * @param jimuReportDto
    * @return
    */
    public QueryWrapper<JimuReportEntity> getWrapper(JimuReportDto jimuReportDto){
        QueryWrapper<JimuReportEntity> wrapper= new QueryWrapper();
        if(jimuReportDto != null){
            JimuReportEntity jimuReportEntity = ConvertUtil.transformObj(jimuReportDto,JimuReportEntity.class);
            wrapper.eq(StrUtil.isNotEmpty(jimuReportEntity.getCode()),"code",jimuReportEntity.getCode());
            wrapper.like(StrUtil.isNotEmpty(jimuReportEntity.getName()),"name",jimuReportEntity.getName());
            wrapper.eq(StrUtil.isNotEmpty(jimuReportEntity.getType()),"type",jimuReportEntity.getType());
        }
        wrapper.ne("type","printinfo");
        wrapper.eq("del_flag",false);

        return wrapper;
    }


    @Override
    public IPage<JimuReportDto> pageJimuReport(Page<JimuReportDto> page, JimuReportDto jimuReportDto, SysUser user) {
        Map<String, Long> pageMap = BizCommonUtil.getPageParam(page);
        String userName=null;
        Long userId=null;
        List<Long> depts = new ArrayList<>();
        if(user!=null && !user.getIsAdmin()){
            userName = user.getLoginName();
            userId = user.getId();
            Arrays.stream(user.getDeptParents().split(",")).forEach(item->{
                depts.add(Long.parseLong(item));
            });
        }
        List<JimuReportEntity> list=baseMapper.selectPageExtends(pageMap,jimuReportDto,userName,userId,depts);
        Long total = baseMapper.countExtends(jimuReportDto,userName,userId,depts);
        Page<JimuReportDto> pageDto = new Page(page.getCurrent(), page.getSize(), total);
        pageDto.setRecords(ConvertUtil.transformObjList(list, JimuReportDto.class));
        return pageDto;
    }


    @Override
    public List<JimuReportDto> listJimuReport(JimuReportDto jimuReportDto){
        QueryWrapper<JimuReportEntity> wrapper = getWrapper(jimuReportDto);
        List<JimuReportEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,JimuReportDto.class);
    }

    @Override
    public boolean delById(String id) {
        return baseMapper.delById(id) >0;
    }

    @Override
    public boolean delByIds(List<String> ids) {
        return baseMapper.delByIds(ids) >0;
    }
}

